/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package origendatos.dyd.model;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import origendatos.dyd.model.exceptions.NonexistentEntityException;
import origendatos.dyd.model.exceptions.PreexistingEntityException;

/**
 *
 * @author erodrig
 */
public class CaracteristicasPjJpaController {

    public CaracteristicasPjJpaController() {
        emf = Persistence.createEntityManagerFactory("OrigenDatosPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(CaracteristicasPj caracteristicasPj) throws PreexistingEntityException, Exception {
        if (caracteristicasPj.getCaracteristicasPjPK() == null) {
            caracteristicasPj.setCaracteristicasPjPK(new CaracteristicasPjPK());
        }
        caracteristicasPj.getCaracteristicasPjPK().setIdCaracteristica(caracteristicasPj.getCaracteristicas().getAbreviatura());
        caracteristicasPj.getCaracteristicasPjPK().setIdPj(caracteristicasPj.getPj().getId());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Pj pj = caracteristicasPj.getPj();
            if (pj != null) {
                pj = em.getReference(pj.getClass(), pj.getId());
                caracteristicasPj.setPj(pj);
            }
            Caracteristicas caracteristicas = caracteristicasPj.getCaracteristicas();
            if (caracteristicas != null) {
                caracteristicas = em.getReference(caracteristicas.getClass(), caracteristicas.getAbreviatura());
                caracteristicasPj.setCaracteristicas(caracteristicas);
            }
            em.persist(caracteristicasPj);
            if (pj != null) {
                pj.getCaracteristicasPjList().add(caracteristicasPj);
                pj = em.merge(pj);
            }
            if (caracteristicas != null) {
                caracteristicas.getCaracteristicasPjList().add(caracteristicasPj);
                caracteristicas = em.merge(caracteristicas);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCaracteristicasPj(caracteristicasPj.getCaracteristicasPjPK()) != null) {
                throw new PreexistingEntityException("CaracteristicasPj " + caracteristicasPj + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CaracteristicasPj caracteristicasPj) throws NonexistentEntityException, Exception {
        caracteristicasPj.getCaracteristicasPjPK().setIdCaracteristica(caracteristicasPj.getCaracteristicas().getAbreviatura());
        caracteristicasPj.getCaracteristicasPjPK().setIdPj(caracteristicasPj.getPj().getId());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CaracteristicasPj persistentCaracteristicasPj = em.find(CaracteristicasPj.class, caracteristicasPj.getCaracteristicasPjPK());
            Pj pjOld = persistentCaracteristicasPj.getPj();
            Pj pjNew = caracteristicasPj.getPj();
            Caracteristicas caracteristicasOld = persistentCaracteristicasPj.getCaracteristicas();
            Caracteristicas caracteristicasNew = caracteristicasPj.getCaracteristicas();
            if (pjNew != null) {
                pjNew = em.getReference(pjNew.getClass(), pjNew.getId());
                caracteristicasPj.setPj(pjNew);
            }
            if (caracteristicasNew != null) {
                caracteristicasNew = em.getReference(caracteristicasNew.getClass(), caracteristicasNew.getAbreviatura());
                caracteristicasPj.setCaracteristicas(caracteristicasNew);
            }
            caracteristicasPj = em.merge(caracteristicasPj);
            if (pjOld != null && !pjOld.equals(pjNew)) {
                pjOld.getCaracteristicasPjList().remove(caracteristicasPj);
                pjOld = em.merge(pjOld);
            }
            if (pjNew != null && !pjNew.equals(pjOld)) {
                pjNew.getCaracteristicasPjList().add(caracteristicasPj);
                pjNew = em.merge(pjNew);
            }
            if (caracteristicasOld != null && !caracteristicasOld.equals(caracteristicasNew)) {
                caracteristicasOld.getCaracteristicasPjList().remove(caracteristicasPj);
                caracteristicasOld = em.merge(caracteristicasOld);
            }
            if (caracteristicasNew != null && !caracteristicasNew.equals(caracteristicasOld)) {
                caracteristicasNew.getCaracteristicasPjList().add(caracteristicasPj);
                caracteristicasNew = em.merge(caracteristicasNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                CaracteristicasPjPK id = caracteristicasPj.getCaracteristicasPjPK();
                if (findCaracteristicasPj(id) == null) {
                    throw new NonexistentEntityException("The caracteristicasPj with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(CaracteristicasPjPK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CaracteristicasPj caracteristicasPj;
            try {
                caracteristicasPj = em.getReference(CaracteristicasPj.class, id);
                caracteristicasPj.getCaracteristicasPjPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The caracteristicasPj with id " + id + " no longer exists.", enfe);
            }
            Pj pj = caracteristicasPj.getPj();
            if (pj != null) {
                pj.getCaracteristicasPjList().remove(caracteristicasPj);
                pj = em.merge(pj);
            }
            Caracteristicas caracteristicas = caracteristicasPj.getCaracteristicas();
            if (caracteristicas != null) {
                caracteristicas.getCaracteristicasPjList().remove(caracteristicasPj);
                caracteristicas = em.merge(caracteristicas);
            }
            em.remove(caracteristicasPj);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<CaracteristicasPj> findCaracteristicasPjEntities() {
        return findCaracteristicasPjEntities(true, -1, -1);
    }

    public List<CaracteristicasPj> findCaracteristicasPjEntities(int maxResults, int firstResult) {
        return findCaracteristicasPjEntities(false, maxResults, firstResult);
    }

    private List<CaracteristicasPj> findCaracteristicasPjEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(CaracteristicasPj.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public CaracteristicasPj findCaracteristicasPj(CaracteristicasPjPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CaracteristicasPj.class, id);
        } finally {
            em.close();
        }
    }

    public int getCaracteristicasPjCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<CaracteristicasPj> rt = cq.from(CaracteristicasPj.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
